// pages/healthcard/healthcard.js
import {
  cardList,
  activation,
} from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: "../../static/images/activation.png",
    cardimg1: "../../static/images/健康卡.png",
    cardimg2: "../../static/images/wine.png",
    cardimg3: "../../static/images/购物卡.png",
    storeId: '',
    cardLists: [],
    cardNum: '',
    password: '',
    disabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      storeId: options.storeId
    })
    this.getList()
  },
  getList() {
    cardList(this.data.storeId).then(res => {
      this.setData({
        cardLists: res
      })
    })
  },
  getcard(e) {
    this.setData({
      cardNum: e.detail.value
    })
  },
  getpwd(e) {
    this.setData({
      password: e.detail.value
    })
  },
  handleActivate() {
    const {
      cardNum,
      password,
      storeId,
      disabled
    } = this.data
    if (cardNum && password && storeId && !disabled) {
      this.setData({
        disabled: true
      })
      activation({
        cardNum,
        password,
        storeId
      }).then(() => {
        wx.showToast({
          title: '激活成功',
          icon: 'none'
        })
        this.setData({
          disabled: false
        })
        this.getList()
      }).catch(() => {
        this.setData({
          disabled: false
        })
      })
    } else {
      wx.showToast({
        title: !cardNum ? '请输入激活卡号' : !password ? '请输入密码' : '',
        icon: 'none'
      })
    }
  },
  toVerify(e) {
    wx.navigateTo({
      url: '/pages/healthVerify/index?storeId=' + this.data.storeId + '&cardNum=' + e.currentTarget.dataset.card,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})